<?php
/** 
 * 相册控制器(后台)
 * 
 * @author      sinalma  <sinalma@foxmail.com>
 * @version     1.0 版本号
 */
namespace app\admin\controller;

use app\admin\model\Album as AlbumModel;
use app\admin\controller\Base;

/** 
 * Class Album
 * 控制器 
 *
 * @package app\index\controller
 * @author      sinalma  <sinalma@foxmail.com>
 * @version     1.0 版本号
 */ 
class Album extends Base
{
	
    /**  
     * 插入照片数据
     * 
     * @access public 
     * @return array 数组
     */	
	public function insertPic(){
		$photo = new AlbumModel($_POST);
		return $photo->allowField(true)->save();
	}

    /**  
     * 删除指定照片
     * 
     * @access public
     * @param  pic_id POST参数  照片id
     * @return bool
     */	
	public function delete(){
		$pic_id = input('param.pic_id');
		$res = AlbumModel::destroy($pic_id);
		return $res;
	}


    /**  
     * 删除指定照片
     * TODO allowField不好使
     * 
     * @access public
     * @param  pic_id POST参数  照片id
     * @return 影响的行数
     */	
	public function updatePic(){
		$pic_id = input('param.pic_id');
		$photo = new AlbumModel();
		// 为什么allowfield的字段的值一定要修改了,才能成功
		$res = $photo->allowField(['name','link','small_link','type','time','device','note','address'])->save($_POST, ['pic_id' => $pic_id]);
		return $res;
	}

    /**  
     * 更新照片信息(页面)
     * 
     * @access public
     * @param  pic_id POST参数  照片id
     * @return view
     */	
	public function update(){
		$pic_id = input('param.pic_id');
		$photo = AlbumModel::where("pic_id",$pic_id)->select();
		$this->assign('list',$photo);
		$this->assign('pic_id',$pic_id);
		return $this->fetch(); 
	}

 	/**  
     * 获取所有照片(页面)
     * 
     * @access public
     * @return view
     */	
	public function index(){
		$list = AlbumModel::where('state',1)->paginate(6);
		$this->assign('list',$list);
		$this->assign('page',$list->render());
		return $this->fetch();
	}


	/**  
     * 插入照片的页面
     * 
     * @access public
     * @return view
     */	
	public function insert(){
		$this->assign('list','');
		return $this->fetch();
	}
}